<?php

class IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */

        $this->view->usuario = array(
            'name' => 'Jean Brito',
            'terminal' => $_SERVER['REMOTE_ADDR'],
            'image' => 'fotoUsuario.jpg'
        );
    }

    public function indexAction() {
        // action body
        $this->view->ExecutarFuncao = 'utilitariosPapeisListar';
    }

    public function viewAction() {
        // action body
        $this->view->layout()->disableLayout();
        $modelName = 'Application_Model_' . $this->getRequest()->getParam('Model');
        $model = new $modelName();

        $this->view->modelName = $this->getRequest()->getParam('Model');
        $modelName = 'Application_Model_' . $this->view->modelName;
        $this->view->model = new $modelName();
        $this->view->package = 'Index.View.Model.' . $this->view->modelName;
        $this->view->ViewLayer = "AgenteNET." . $this->view->package . ".List";
    }

    public function storeAction() {
        // action body
        $this->view->layout()->disableLayout();
        $this->view->modelName = $this->getRequest()->getParam('Model');
        $modelName = 'Application_Model_' . $this->view->modelName;
        $this->view->model = new $modelName();
        $this->view->package = 'Index.Store.Model.' . $this->getRequest()->getParam('Model');
    }

    public function dataAction() {
        // action body

        $this->view->ViewLayer = 'AgenteNET.Utilitarios.Usuarios.View.List';
        $ajaxContext = $this->_helper->contextSwitch();
        $ajaxContext->setAutoJsonSerialization(true);
        $ajaxContext
                ->addActionContext('Data', array('json'))
                ->initContext('json');

        $modelName = 'Application_Model_' . $this->getRequest()->getParam('Model');
        $model = new $modelName();
        $method = $this->getRequest()->getParam('method');

        $data = Zend_Json::decode($this->getRequest()->getRawBody());
        $id = $this->getRequest()->getParam('id');
        if ($method == "post")
            $this->view->assign($model->post($data));
        elseif ($method == "put")
            $this->view->assign($model->put($id, $data));
        elseif ($method == "delete")
            $this->view->assign($model->delete($id));
        elseif ($id == "") {
            echo Zend_Json::encode($model->index($this->getRequest()->getParam('filter'), $this->getRequest()->getParam('start'), $this->getRequest()->getParam('limit'), $this->getRequest()->getParam('sort')), false, array('enableJsonExprFinder' => true));
            exit;
            //$this->view->assign($model->index($this->getRequest()->getParam('filter'), $this->getRequest()->getParam('start'), $this->getRequest()->getParam('limit'), $this->getRequest()->getParam('sort')));
        } else {
            $this->view->assign($model->get($id));
        }
    }

}